package util

import (
	"gpt-api/framework/log"
	"io/ioutil"
	"net/http"
	"strings"
)

func TuiAd(IP string, UA string, Pid string, high string, width string) ([]byte, error) {
	var str string
	IDs := RoundNum()
	//Pid = "438544"
	//url := "https://adx.tuia-pre.cn/bid/tuia"
	url := "https://adx.tuia.cn/bid/tuia"
	method := "POST"
	str = "{\n\t\"device\": {\n\t\t\"ip\": \"" +
		IP + "\",\n\t\t\"ua\": \"" + UA + "\"\n\t},\n\t\"id\": \"" + IDs +
		"\",\n\t\"imp\": [\n\t\t{\n\t\t\t\"banner\": {\n\t\t\t\t\"h\": " +
		high +
		",\n\t\t\t\t\"mimes\": [\n\t\t\t\t\t\"jpg\",\n\t\t\t\t\t\"jpeg\",\n\t\t\t\t\t\"png\",\n\t\t\t\t\t\"gif\"\n\t\t\t\t],\n\t\t\t\t\"w\": " +
		width +
		"\n\t\t\t},\n\t\t\t\"bidfloor\": 0,\n\t\t\t\"id\": \"1\",\n\t\t\t\"pid\": \"" +
		Pid +
		"\",\n\t\t\t\"type\": 1\n\t\t}\n\t],\n\t\"secure\": 1\n}"
	payload := strings.NewReader(str)
	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)
	if err != nil {
		log.Error("TuiAd请求失败", log.Var("err", err))
		return nil, err
	}
	req.Header.Add("User-Agent", UA)
	req.Header.Add("Content-Type", "application/json")
	res, err := client.Do(req)
	if err != nil {
		log.Error("TuiAd请求失败", log.Var("err", err))
		return nil, err
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Error("推啊res解析失败", log.Var("err", err))
		return nil, err
	}

	return body, err
}
